<template>
	<view class="game">
		<view class="game_header">
			<view class="tabs">
				<block v-for="item in tabs" :key="item.val">
					<view class="tab_item" :class="{'current_tab':currentTab==item.val}" @tap="tabsChange(item)">
						{{item.name}}
					</view>
				</block>
			</view>
			<view class="flex a-c search-box">
				<image src="/static/img/search-gray.png" mode="widthFix" class="icon-26" />
				<input type="text" v-model="search" placeholder="搜索" placeholder-class="p-c" @confirm="getGameList">
			</view>
		</view>
		<!-- 本周赛事 -->
		<view class="current_game" v-if="currentTab==0">
			<block v-for="(item,index) in weekGame" :key="index">
				<view class="item" @tap="handleDetail(item)">
					<view class="game_status" :class="{'game_status2':item.activityStatus==0}">
						{{item.activityStatus==0?'未开始':item.activityStatus==1?'进行中':'已结束'}}</view>
					<image :src="item.image" mode="aspectFill" class="cover_img" />
					<view class="game_info">
						<view class="info_1">
							<view class="game_title">{{ item.storeName }}</view>
							<view class="price">
								<text>￥</text>
								<text class="price_val">{{ item.price }}</text>
							</view>
						</view>
						<view class="info_2">
							<view class="info_2_l">
								<view class="">时间：{{ item.activityStartTime }}</view>
								<view class="">地点：{{ item.address }}</view>
							</view>
							<view class="join_btn" v-if="item.applyStatus==1"  @tap="handleDetail(item)">
								<text>立即参与</text>
								<view class="">GO</view>
							</view>
							<view class="join_btn" v-if="item.applyStatus==0||item.applyStatus==2"  :class="{'disable_join ':item.applyStatus==0||item.applyStatus==2}" >
								<text>立即参与</text>
								<view class="">GO</view>
							</view>
						</view>
					</view>
				</view>
			</block>
			<block v-if="!weekGame || !weekGame.length">
				<view class="noMore">暂无赛事</view>
			</block>
		</view>
		<!-- 往期赛事 -->
		<view class="past_game" v-else-if="currentTab==1">
			<block v-for="(item,index) in previousEvents" :key="index">
				<view class="item">
					<image class="top_icon" src="/static/home/top1.png" mode="" v-if="index===0"></image>
					<image class="top_icon" src="/static/home/top2.png" mode="" v-if="index===1"></image>
					<image class="top_icon" src="/static/home/top3.png" mode="" v-if="index===2"></image>
					<view class="game_info">
						<image class="cover_img" :src="item.image" mode="aspectFill"></image>
						<view class="">
							<view class="title">{{ item.storeName }}</view>
							<view class="time">时间：{{ item.activityStartTime }}</view>
							<view class="join_count">
								<text>参与人数：</text>
								<text class="count_val">{{ item.sales || 0 }}</text>
							</view>
						</view>
					</view>
					<view class="address">
						<uni-icons type="location-filled" color="#999" size="16"></uni-icons>
						<text>{{ item.address }}</text>
					</view>
				</view>
			</block>
			<block v-if="!previousEvents || !previousEvents.length">
				<view class="noMore">暂无赛事</view>
			</block>
		</view>
		<!-- 作品 -->
		<view class="works" v-else>
			<view class="sub_tabs">
				<block v-for="item in subTabs" :key="item.val">
					<view :class="{'current_sub_tab':currentSubTab==item.val}" @tap="tapSub(item)">{{item.name}}</view>
				</block>
			</view>
			<view class="releaseBtn">
				<image src="/static/home/fadongtai.png" @click="$to('/pages/index/release/release?pagetype=赛事')">
				</image>
			</view>

			<block v-for="(v,i) in worksList" :key="i">
				<view class="item" @click="$to(`/pages/index/workShowDetail?id=${v.id}`)">
					<image class="top_icon" src="/static/home/top1.png" mode="" v-if="i==0"></image>
					<image class="top_icon" src="/static/home/top2.png" mode="" v-if="i==1"></image>
					<image class="top_icon" src="/static/home/top3.png" mode="" v-if="i==2"></image>
					<view class="item_t">
						<view class="author">
							<image :src="v.userAvatar" mode=""></image>
							<view class="">
								<view class="author_name">{{ v.userName }}</view>
								<view class="time">{{ v.createTime }}</view>
							</view>
						</view>
					</view>
					<view class="item_m">
						<view class="article_text">{{ v.content }}</view>
						<view class="article_imgs">
							<block v-for="img in v.imagesList" :key="img">
								<image :src="img" mode="aspectFill" @tap.stop="previewImg(v.imagesList)"></image>
							</block>
						</view>
					</view>
					<view class="item_b">浏览量{{ v.browse }}</view>
				</view>
			</block>
			<block v-if="!worksList || !worksList.length">
				<view class="noMore">{{ `暂无${currentSubTab===0?'家长作品':'老师作品'}` }}</view>
			</block>
		</view>
		
		<view style="text-align: center;color: #999;font-size: 24rpx;" v-if="noMoreData&&(worksList.length!=0 || previousEvents.length!=0)">
			没有更多啦
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tabs: [
					{
						name: '本周赛事',
						val: 0
					}, {
						name: '往期赛事',
						val: 1
					}, {
						name: '作品展示',
						val: 3
					},
				],
				currentTab: 0,
				subTabs: [
					{
						name: '老师作品',
						val: 1
					}, {
						name: '家长作品',
						val: 0
					},
				],
				currentSubTab: 1,
				search: '',
				gameData: {
					activity: 0,
					keywords: '',
					limit: 10,
					page: 1,
					search: '',
					type: 0,
					isBest: false,
				},
				worksShowData: {
					// startTime: '',
					// endTime: '',
					limit: 10,
					page: 1,
					search: '',
					// userName: '',
					userType: 0
				},
				weekGame: [],
				previousEvents: [],
				worksList: [],
				applyData: {
					attValueId: 0,
					preOrderType: 0,
					storeProductId: 0
				},
				totalPage: 1,
				noMoreData: false,
			};
		},
		onShow() {
			if(this.currentTab == 3) {
				this.gameData.page = 1;
				this.getWorksShow();
			}else {
				this.getGameList();
			}
		},
		onLoad() {
		},
		methods: {
			previewImg(imgList) {
				uni.previewImage({
					current: 0,
					urls: imgList,
				});
			},
			tabsChange(item) {
				this.currentTab = item.val;
				if (item.val == 3) {
					this.worksShowData.page = 1;
					this.worksShowData.userType = 1;
					this.getWorksShow();
				} else {
					this.gameData.page = 1;
					this.gameData.type = item.val;
					this.getGameList();
				}
			},
			handleDetail(v) {
				this.$to(`/pages/index/gameDetail/gameDetail?id=${v.id}`)
				// if(v.applyStatus==1){
					
				// }else if(v.applyStatus==0){
				// 	this.showToast('报名暂未开始~')
				// }else{
				// 	this.showToast('报名活动时间已结束~')
				// }
		
			},
			release() {
				uni.navigateTo({
					url: '/pages/index/release/release'
				})
			},
			async getGameList() {
				this.gameData.search = this.search;
				const res = await this.$api.get('competition/week/list', this.gameData);
				if (res.code == 200) {
					if (this.currentTab === 0) {
						if (this.gameData.page == 1) {
							this.weekGame = res.data.list;
							this.totalPage = res.data.totalPage;
						} else {
							this.weekGame.push(...res.data.list);
						}
					} else if (this.currentTab === 1) {
						if (this.gameData.page == 1) {
							this.previousEvents = res.data.list;
							this.totalPage = res.data.totalPage;
						} else {
							this.previousEvents.push(...res.data.list);
						}
						// this.previousEvents = res.data.list;
					}
				}
			},
			async getWorksShow() {
				this.worksShowData.search = this.search;
				const res = await this.$api.get('competition/work/show', this.worksShowData);
				if (res.code == 200) {
					if (this.worksShowData.page === 1) {
						this.worksList = res.data;
					} else {
						this.worksList.push(...res.data);
					}
					if(res.data && res.data.length) {
						var len = res.data.length;
						if(res.data.length<10) {
							this.noMoreData = true;
						}
					}
				}
			},
			tapSub(item) {
				this.currentSubTab = item.val;
				this.worksShowData.userType = item.val;
				this.getWorksShow();
			},
			async toApply(id) {
				this.applyData.storeProductId = id;
				const res = await this.$api.post('order/pre/order', this.applyData);
				if (res.code == 200) {
					this.$to(`/pages/index/joinGame/joinGame?preOrderNo=${res.data.preOrderNo}`)
				}
			},

		},
		onReachBottom() {
			if (this.currentTab === 3 && !this.noMoreData) {
				this.worksShowData.page++;
				this.getWorksShow();
			} else if(this.currentTab === 0 ||this.currentTab === 1){
				if(this.gameData.page<this.totalPage) {
					this.gameData.page++;
					this.getGameList();
				}else {
					this.noMoreData = true;
				}
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #F7F7F7;
	}

	.releaseBtn {
		z-index: 9;
		position: fixed;
		top: 60vh;
		right: 30rpx;

		image {
			width: 100rpx;
			height: 100rpx;
		}
	}

	.p-c {
		color: #999;
	}

	.game {
		position: relative;

		.game_header {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 26rpx;
			box-sizing: border-box;
			position: fixed;
			left: 0;
			right: 0;
			top: 0;
			z-index: 9;
			background-color: #fff;

			.tabs {
				flex: 1;
				display: flex;
				justify-content: space-between;
				align-items: center;
				margin-right: 44rpx;

				.tab_item {
					font-size: 26rpx;
				}

				.current_tab {
					font-size: 30rpx;
					font-weight: bold;
					position: relative;
				}

				.current_tab::before {
					height: 8rpx;
					width: 100%;
					background-image: linear-gradient(to right, #3DD8FF, #589AFF);
					position: absolute;
					display: block;
					content: '';
					bottom: 0;
					left: 0;
					z-index: -1;
				}
			}

			.search-box {
				background-color: #F7F7F7;
				border-radius: 30rpx;
				font-size: 27rpx;
				padding: 0 25rpx;
				height: 60rpx;
				line-height: 60rpx;

				.icon-26 {
					width: 26rpx;
					height: 26rpx;
					margin-right: 14rpx;
				}

				input {
					line-height: 60rpx;
					width: 160rpx;
					font-size: 26rpx;
				}
			}

			// .search-box {
			// 	display: flex;
			// 	align-items: center;
			// 	justify-content: center;
			// 	width: 166rpx;
			// 	height: 60rpx;
			// 	border-radius: 30rpx;
			// 	background-color: #F7F7F7;

			// 	image {
			// 		width: 26rpx;
			// 		height: 26rpx;
			// 		margin-right: 14rpx;
			// 	}

			// 	text {
			// 		font-size: 26rpx;
			// 		color: #999;
			// 	}
			// }
		}

		.current_game {
			padding: 130rpx 26rpx 30rpx;
			box-sizing: border-box;

			.item {
				width: 100%;
				border-radius: 20rpx;
				overflow: hidden;
				background-color: #fff;
				margin-bottom: 20rpx;
				position: relative;

				.cover_img {
					width: 100%;
					height: 333rpx;
				}

				.game_status {
					width: 106rpx;
					height: 46rpx;
					border-radius: 20rpx 0 20rpx 0;
					background-color: #2E80FE;
					color: #fff;
					font-size: 24rpx;
					text-align: center;
					line-height: 46rpx;
					position: absolute;
					left: 0;
					top: 0;
					z-index: 2;
				}

				.game_status2 {
					background-color: #F82929;
				}

				.info_1 {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 22rpx 26rpx 30rpx;
					box-sizing: border-box;

					.game_title {
						font-size: 32rpx;
						font-weight: bold;
					}

					.price {
						font-size: 24rpx;
						font-weight: bold;
						color: #FF0404;

						.price_val {
							font-size: 40rpx;
						}
					}
				}

				.info_2 {
					display: flex;
					justify-content: space-between;
					align-items: center;
					padding: 0 26rpx 22rpx;
					box-sizing: border-box;

					.info_2_l {
						color: #999;
						font-size: 24rpx;
						display: flex;
						flex-direction: column;
						justify-content: space-between;
						height: 66rpx;
						line-height: 24rpx;
					}

					.join_btn {
						width: 200rpx;
						height: 66rpx;
						border-radius: 33rpx;
						background: linear-gradient(to right, #3DD8FF, #589AFF);
						display: flex;
						align-items: center;
						justify-content: flex-end;
						padding: 0 4rpx;
						box-sizing: border-box;

						text {
							font-size: 26rpx;
							color: #fff;
						}

						>view {
							width: 60rpx;
							height: 60rpx;
							border-radius: 50%;
							background-color: #fff;
							text-align: center;
							line-height: 60rpx;
							font-size: 26rpx;
							margin-left: 12rpx;
							font-weight: bold;
							color: #579CFF;
						}
					}

					.disable_join {
						background-color: #E6E6E6;
						background: linear-gradient(to right, #E6E6E6, #E6E6E6);

						text {
							color: #999999;
						}

						>view {
							background-color: #CCCCCC;
							color: #999999;
						}
					}
					// .disable_joinGray{
					// 	opacity: 0.3;
					// }
				}
			}
		}

		.past_game {
			padding: 130rpx 26rpx 30rpx;
			box-sizing: border-box;

			.item {
				width: 100%;
				padding: 20rpx 26rpx 26rpx;
				box-sizing: border-box;
				background-color: #fff;
				border-radius: 20rpx;
				margin-bottom: 20rpx;
				position: relative;

				.top_icon {
					width: 60rpx;
					height: 73rpx;
					position: absolute;
					top: 0;
					left: 26rpx;
					z-index: 2;
				}

				.game_info {
					display: flex;
					align-items: flex-start;
					padding-bottom: 14rpx;
					border-bottom: 1rpx dashed #F0F0F0;
					margin-bottom: 26rpx;

					.cover_img {
						width: 200rpx;
						height: 160rpx;
						border-radius: 14rpx;
						margin-right: 16rpx;
					}

					>view {
						flex: 1;

						.title {
							width: 420rpx;
							font-size: 30rpx;
							line-height: 30rpx;
							font-weight: bold;
							color: #1A1A1A;
							overflow: hidden;
							white-space: nowrap;
							text-overflow: ellipsis;
						}

						.time {
							font-size: 24rpx;
							line-height: 24rpx;
							color: #999999;
							padding: 36rpx 0 22rpx;
						}

						.join_count {
							font-size: 24rpx;
							line-height: 24rpx;
							color: #999999;

							.count_val {
								color: #2E80FE;
								font-weight: bold;
							}
						}
					}
				}

				.address {
					display: flex;
					align-items: center;
					font-size: 24rpx;
					color: #999999;
				}
			}
		}

		.works {
			padding: 230rpx 26rpx 30rpx;
			box-sizing: border-box;

			.sub_tabs {
				width: 100%;
				height: 100rpx;
				position: fixed;
				left: 0;
				top: 112rpx;
				z-index: 6;
				background-color: #F7F7F7;
				display: flex;
				align-items: center;
				padding: 0 26rpx;
				box-sizing: border-box;

				>view {
					width: 166rpx;
					height: 54rpx;
					border-radius: 27rpx;
					text-align: center;
					line-height: 54rpx;
					background-color: #F0F0F0;
					color: #999999;
					font-size: 26rpx;
					margin-right: 52rpx;
				}

				.current_sub_tab {
					background-color: #E8F1FF;
					color: #2E80FE;
					font-weight: bold;
				}
			}

			.release_btn {
				width: 114rpx;
				height: 114rpx;
				position: fixed;
				right: 46rpx;
				bottom: 760rpx;
				z-index: 9;
			}

			.item {
				padding: 16rpx 26rpx 20rpx;
				box-sizing: border-box;
				border-radius: 20rpx;
				background-color: #fff;
				margin-bottom: 20rpx;
				position: relative;

				.top_icon {
					width: 60rpx;
					height: 73rpx;
					position: absolute;
					top: 0;
					right: 26rpx;
					z-index: 2;
				}

				.item_t {
					display: flex;
					justify-content: space-between;

					.author {
						display: flex;
						align-items: center;

						image {
							width: 80rpx;
							height: 80rpx;
							border-radius: 50%;
							margin-right: 14rpx;
						}

						.author_name {
							font-size: 30rpx;
							line-height: 30rpx;
							font-weight: bold;
							margin-bottom: 14rpx;
						}

						.time {
							font-size: 24rpx;
							line-height: 24rpx;
							color: #B3B3B3;
						}
					}
				}

				.item_m {
					padding: 20rpx 0;

					.article_text {
						font-size: 26rpx;
						line-height: 40rpx;
						margin-bottom: 16rpx;
					}

					.article_imgs {
						image {
							width: 206rpx;
							height: 200rpx;
							border-radius: 20rpx;
						}

						image:not(:last-child) {
							margin-right: 12rpx;
						}
					}
				}

				.item_b {
					font-size: 24rpx;
					color: #999999;
					line-height: 24rpx;
				}
			}
		}
	}
</style>